#include <CountDownLatch.h>

CountDownLatch::CountDownLatch(int count)
: m_count(count)
{

}

//等待直到count_=0
void CountDownLatch::wait()
{
    std::unique_lock<std::mutex> lock(m_mutex);
    while(m_count>0)
    {
        m_condition.wait(lock);
    }
}

//对count_做减1操作，若coun=0则唤醒所有等待线程
void CountDownLatch::countDown()
{
    std::unique_lock<std::mutex> lock(m_mutex);
    --m_count;
    if(0==m_count)
    {
        m_condition.notify_all();
    }
}

//返回 count_,记得带const，函数中不能修改任何成员变量 
//注意 mutex_声明成mutable是为了突破 const的限制
int CountDownLatch::getCount() const
{
    std::unique_lock<std::mutex> lock(m_mutex);
    return m_count;
}

